
package nl.hanze.shoestring.ui;

import com.toedter.calendar.JDateChooser;
import com.toedter.components.JSpinField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;


/**
 *  UI Helper bevat generieke methoden om GUI schermcomponenten te creeren
 *  static, dus class in aanroep
 * 
 * @author bleker
 */
public class UIHelper {
    
    /**
     * 
     */
    public UIHelper() {
    }
    
    /*
     * Creeer JLabel en add to Panel
     */
    public static JLabel createLabel(JLabel label, String txt, int x, int y, int w, int h, JPanel panel) {
        label.setText(txt);
        label.setBounds(x, y, w, h);
        panel.add(label);
        return label;
    }

    
    /*
     * Creeer JTextField en add to Panel
     */
    public static JTextField createTextField(JTextField txtField, String txt, int x, int y, int w, int h, JPanel panel) {
        txtField.setText(txt);
        txtField.setBounds(x, y, w, h);
        panel.add(txtField);
        return txtField;
    }
    
    /*
     * Creeer JDateChooser en add to Panel
     */
    public static JDateChooser createDateChooser(JDateChooser calField, Date txt, int x, int y, int w, int h, JPanel panel) {
        calField.setDate(txt);
        calField.setBounds(x, y, w, h);
        panel.add(calField);
        return calField;
    }

    /*
     * Creeer JSpinField (numeric value) en add to Panel
     * @param alignment uitrichting - SwingConstants
     */
    public static JSpinField createSpinField(JSpinField spinField, Integer val, int x, int y, int w, int h, JPanel panel, int alignment) {
        spinField.setValue(val);
        spinField.setBounds(x, y, w, h);
        spinField.setHorizontalAlignment(alignment);
        panel.add(spinField);
        return spinField;
    }

    /*
     * Creeer JTextField en add to Panel
     */
    public static JCheckBox createCheckBox(JCheckBox chkBox, String txt, int x, int y, int w, int h, JPanel panel) {
        chkBox.setText(txt);
        chkBox.setBounds(x, y, w, h);
        panel.add(chkBox);
        return chkBox;
    }
    
    /**
     * method: create a JButton at a specified location in the JPanel
     * @param button 
     * @param txt labeltext
     * @param x xpos
     * @param y ypos
     * @param w width
     * @param h heigth
     * @param panel 
     * @return JButton
     */
    public static JButton createButton(JButton button, String txt, int x, int y, int w, int h, JPanel panel)
    {
        //Label een text meegeven
        button.setText(txt);
        //Label juiste positie geven
        button.setBounds(x,y,w,h);
        //Label koppelen aan het panel
        panel.add(button); 
        //Label teruggeven
        return button;
    }
    
    public static JComboBox createCombo(JComboBox combo, int x, int y, int w, int h, JPanel panel)
    {
        //Label juiste positie geven
        combo.setBounds(x,y,w,h);
        //Label koppelen aan het panel
        panel.add(combo); 
        //Label teruggeven
        return combo;
    }
    
    /*
     * Voeg Navigatie buttons toe aan Overzichtstabel mbv een JPanel component
     * 
     * @param pNavigation
     * @param table de getoonde JTable
     * @param scrollPaneRes Scrollpane van JTable
     * @param numberOfRows aantal getoonde rows
     */
    public static void addNavigation(final JPanel pNavigation, final JTable table, final JScrollPane scrollPaneRes,final int numberOfRows) {
        pNavigation.setLayout(new FlowLayout(FlowLayout.CENTER));

        JButton next = new JButton(">");
        next.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int height = table.getRowHeight() * (numberOfRows - 1);
                JScrollBar bar = scrollPaneRes.getVerticalScrollBar();
                bar.setValue(bar.getValue() + height);
            }
        });
        JButton previous = new JButton("<");
        previous.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                int height = table.getRowHeight() * (numberOfRows - 1);
                JScrollBar bar = scrollPaneRes.getVerticalScrollBar();
                bar.setValue(bar.getValue() - height);
            }
        });
        JButton begin = new JButton("<<");
        begin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                JScrollBar bar = scrollPaneRes.getVerticalScrollBar();
                bar.setValue(bar.getMinimum());
            }
        });
        JButton end = new JButton(">>");
        end.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                JScrollBar bar = scrollPaneRes.getVerticalScrollBar();
                bar.setValue(bar.getMaximum());
            }
        });

        pNavigation.add(begin);
        pNavigation.add(previous);
        pNavigation.add(next);
        pNavigation.add(end);
        
    }
}
